package com.ruoyi.recyclecomprehensive.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.recycle.domain.FactoryAnnouncement;
import com.ruoyi.recycle.domain.FactoryManagement;
import com.ruoyi.recycle.domain.HomePageSlideshow;
import com.ruoyi.recycle.domain.SysTypeConfig;
import com.ruoyi.recycle.dto.FactoryManagementDTO;
import com.ruoyi.recycle.service.FactoryAnnouncementService;
import com.ruoyi.recycle.service.IFactoryManagementService;
import com.ruoyi.recycle.service.IHomePageSlideshowService;
import com.ruoyi.recycle.service.ISysTypeConfigService;
import com.ruoyi.recyclecomprehensive.vo.FactoryCategoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/factoryMange")
@RequiredArgsConstructor
@Api(tags = "工厂管理")
public class FactoryController extends BaseController {

    private final IFactoryManagementService factoryManagementService;


    private final FactoryAnnouncementService announcementService;

    private final IHomePageSlideshowService slideshowService;

    private final ISysTypeConfigService sysTypeConfigService;

    @GetMapping("/getCategoryList")
    @ApiOperation("获取工厂分类")
    public R<List<FactoryCategoryVO>> getCategoryList() {
        final List<SysTypeConfig> factoryCategories = sysTypeConfigService.list(Wrappers.lambdaQuery(new SysTypeConfig())
                .eq(SysTypeConfig::getType, "工厂分类"));
        List<FactoryCategoryVO> categoryVOS = new ArrayList<>();
        if (CollUtil.isNotEmpty(factoryCategories)) {
            factoryCategories.forEach(fc -> {
                final FactoryCategoryVO categoryVO = new FactoryCategoryVO();
                categoryVOS.add(categoryVO.setLogo(fc.getImg()).setCategory(fc.getName()));
            });
        }

        return R.ok(categoryVOS);
    }

    @GetMapping("/queryFactorySlideShow")
    @ApiOperation("工厂轮播图")
    public R<List<HomePageSlideshow>> queryFactorySlideShow() {
        return R.ok(slideshowService.list(Wrappers.lambdaQuery(new HomePageSlideshow()).eq(HomePageSlideshow::getType, "1")));
    }

    /**
     * 分页查询工厂列表
     */
    @PostMapping("/factoryPageQuery")
    @ApiOperation("分页查询工厂列表")
    public R<IPage<FactoryManagement>> factoryPageQuery(@RequestBody FactoryManagementDTO queryDTO) {
        final FactoryManagement factory = new FactoryManagement();
        BeanUtils.copyProperties(queryDTO, factory);
        final LambdaQueryWrapper<FactoryManagement> wrapper = Wrappers.lambdaQuery(factory)
                .orderByDesc(FactoryManagement::getCreatedTime);
        if (queryDTO.getSearchTimeBegin() != null) {
            wrapper.ge(FactoryManagement::getCreatedTime, queryDTO.getSearchTimeBegin());
        }
        if (queryDTO.getSearchTimeEnd() != null) {
            wrapper.le(FactoryManagement::getCreatedTime, queryDTO.getSearchTimeEnd());
        }
        final Page<FactoryManagement> page = factoryManagementService.page(new Page<FactoryManagement>()
                .setCurrent(queryDTO.getCurrentPage())
                .setSize(queryDTO.getPageSize()), wrapper);
        final List<FactoryManagement> records = page.getRecords();
        if (CollUtil.isNotEmpty(records)) {
            records.forEach(f -> {
                final String[] split = f.getFactoryImg().split(",");
                if (ArrayUtil.isNotEmpty(split)) {
                    f.setIntroductionImg(split[0]);
                }

            });
            page.setRecords(records);
        }
        return R.ok(page);
    }

    @GetMapping("/queryFactoryDetail")
    @ApiOperation("查询工厂详情")
    public R<FactoryManagement> queryFactoryDetail(@RequestParam Long id) {
        final FactoryManagement factory = factoryManagementService.getById(id);
        if (Objects.nonNull(factory)) {
            final String[] split = factory.getFactoryImg().split(",");
            if (ArrayUtil.isNotEmpty(split)) {
                factory.setIntroductionImg(split[0]);
            }
        }
        return R.ok(factory);
    }

    @GetMapping("/queryAnnouncement")
    @ApiOperation("查询工厂公告")
    public R<List<FactoryAnnouncement>> queryAnnouncement() {
        return R.ok(announcementService.list());
    }

}
